package org.lc;

import com.alibaba.fastjson.JSON;
import org.lc.cmd.Json;
import org.lc.cmd.connect.ClientRequest;
import org.lc.cmd.connect.ClientSession;
import org.lc.cmd.connect.CmdClientConfig;
import org.lc.cmd.connect.impl.DefaultClientProvider;
import org.lc.cmd.connect.impl.DefaultReconnectStrategy;
import org.lc.cmd.connect.impl.ws.WsClient;
import org.lc.cmd.connect.impl.ws.WsSession;
import org.lc.cmd.connect.model.CloseInfo;
import org.lc.cmd.connect.sysreq.ClientDisconnectSentReq;
import org.lc.cmd.model.StyledString;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

// 按两次 Shift 打开“随处搜索”对话框并输入 `show whitespaces`，
// 然后按 Enter 键。现在，您可以在代码中看到空格字符。
public class Main {
    public static void main(String[] args) throws IOException {
        CmdClientConfig.defaultProvider = new DefaultClientProvider() {
            @Override
            public Json jsonUtil() {
                return new Json() {
                    @Override
                    public <T> T parse(String json, Class<T> clazz) {
                        return JSON.parseObject(json, clazz);
                    }

                    @Override
                    public String toJson(Object obj) {
                        return JSON.toJSONString(obj);
                    }
                };
            }
        };
        CmdClientConfig.reconnectStrategy = new DefaultReconnectStrategy() {
            @Override
            protected void showReconnecting(ClientSession session, CloseInfo closeInfo, int retryTimes) {
                System.out.println("服务器" + ((WsSession)session).getUrl() + "断开连接：" + closeInfo.getMessage() + "，正在进行第" + (retryTimes + 1) + "次重试");
            }

            @Override
            protected void showReconnected(ClientSession session) {
                System.out.println("服务器" + ((WsSession)session).getUrl() + "已重连成功");
            }
        };

        WsClient client = new WsClient("test");
        try (ClientSession session = client.createSession("ws://localhost:8080/cmd/ws/test")) {
            session.connect();
            Scanner scanner = new Scanner(System.in);
            while (true) {
                System.out.print("cmd > ");
                String line = scanner.nextLine();
                if (!session.isOpen()) {
                    System.out.println("session closed");
                    break;
                }
                if (handleSysReq(line, session)) {
                    continue;
                }
                ClientRequest request = session.createRequest(line);
                request.setConfirmHandler((data -> {
                    System.out.print(data.getPrompt());
                    return scanner.nextLine();
                }));
                CompletableFuture<StyledString> future = request.send();
                StyledString string = future.get();
                System.out.println(string);
            }
        } catch (ExecutionException | InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    private static boolean handleSysReq(String line, ClientSession session) throws IOException {
        switch (line) {
            case "close":
                ClientDisconnectSentReq req = new ClientDisconnectSentReq(session);
                req.send();
                req.getSession().close();
                return true;
            default:
                return false;
        }
    }
}